﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V = Science.Mathematics.VectorCalculus;

namespace VectorCalculus5Ed.Chapter2.Section3
{
    public class Example06
    {
        public Example06()
		{
		}
		private string result;
		public string Result
		{
			get{return result;}
		}
        public void Compute()
        {
            V.Function.ToLastType<double[], double[]> f
                = new V.Function.ToLastType<double[], double[]>(func);
            double[] x0 = {4.0, 2.0};
          
            V.MatrixOfPartialDerivatives obj = new V.MatrixOfPartialDerivatives(f, x0);
            obj.Compute();

            result += obj.Result[0, 0].ToString() + "    ";
            result += obj.Result[0, 1].ToString() + "\r\n";
            result += obj.Result[1, 0].ToString() + "    ";
            result += obj.Result[1, 1].ToString() + "\r\n";

            result += (Math.Exp(x0[0]+x0[1])).ToString() + "    ";
            result += (Math.Exp(x0[0] + x0[1])+1.0).ToString() + "\r\n";
            result += (x0[1]*x0[1]).ToString() + "    ";
            result += (2.0 * x0[0] * x0[1]).ToString() + "\r\n";

            V.Function.ToLastType<double[], double[]> fc
               = new V.Function.ToLastType<double[], double[]>(funcc);
            double[] t = { 4.0, 2.0, 1.0 };

            V.MatrixOfPartialDerivatives objc = new V.MatrixOfPartialDerivatives(fc, t);
            objc.Compute();

            result += objc.Result[0, 0].ToString() + "    ";
            result += objc.Result[0, 1].ToString() + "    ";
            result += objc.Result[0, 2].ToString() + "\r\n";
            result += objc.Result[1, 0].ToString() + "    ";
            result += objc.Result[1, 1].ToString() + "    ";
            result += objc.Result[1, 2].ToString() + "\r\n";

            result += (t[2]*Math.Exp(t[0])).ToString() + "    ";
            result += (0.0).ToString() + "    ";
            result += (Math.Exp(t[0])).ToString() + "\r\n";
            result += (0.0).ToString() + "    ";
            result += (-Math.Exp(t[2])).ToString() + "    ";
            result += (-t[1]*Math.Exp(t[2])).ToString() + "\r\n";
        }
        private double[] func(double[] x)
        {
            double[] r = new double[2];
            r[0] = Math.Exp(x[0] + x[1]) + x[1];
            r[1] = x[1] * x[1] * x[0];
            return r;
        }
        private double[] funcc(double[] x)
        {
            double[] r = new double[2];
            r[0] = x[2]*Math.Exp(x[0]);
            r[1] = - x[1] * Math.Exp(x[2]);
            return r;
        }
    }
}
/* 
403.428793492627    404.428793492662
3.999999999999    16.0000000000064
403.428793492735    404.428793492735
4    16
54.5981500331566    0    54.5981500331386
0    -2.71828182845971    -5.43656365691873
54.5981500331442    0    54.5981500331442
0    -2.71828182845905    -5.43656365691809
*/

